<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/5/13
 * Time: 9:57
 */

namespace app\index\controller;

use app\index\Common;

class Index extends Common {
    /**
     * 前台首页
     * @return mixed
     */
    public function index(){

        //banner是给到值这个banner变量，这个banner变量是在模板那边使用的
        $this->assign('banner',$this->getBanner());

        return $this->fetch();
    }

    /**
     * 公司介绍
     * @return mixed
     */
    public function about(){
        //seo信息
        $this->seoContent(5);
        return $this->fetch();
    }

    /**
     * 联系我们
     * @return mixed
     */
    public function contact(){
        //seo信息
        $this->seoContent(6);
        return $this->fetch();
    }

    /**
     * FAQ
     * @param int $page
     * @param int $limit
     * @return mixed|\think\response\Json
     */
    public function faq($page = 1,$limit = 100){
        if (is_post()){
            $where[] = ['is_del','=',0];
            $where[] = ['status','=',1];
            $count = db('faq')->where($where)->count();
            $list = db('faq')
                ->where($where)
                ->page($page,$limit)
                ->order('sort','asc')
                ->select();
            foreach ($list as $k => $v){
                $list[$k]['sort'] = ($page - 1) * $limit + $k + 1;
            }
            $pages = ceil($count/$limit);
            return json(['code'=>'suc','data'=>$list,'pages'=>$pages]);
        }
        $this->assign('cur_tab_name','Q&A');
        $this->assign('check_tab',3);
        $this->seoContent(23);
        return $this->fetch();
    }

    /**
     * 服务条款
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function logisticsPolicy(){
        $info = db('policy')
            ->where('id',2)
            ->find();
        $this->assign('info',$info);
        $this->assign('seo_title', $info['seo_title']);
        $this->assign('seo_keywords',$info['seo_keywords']);
        $this->assign('seo_describe',$info['seo_describe']);
        return $this->fetch();
    }

    /**
     * 隐私条款
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function privacyPolicy(){
        $info = db('policy')
            ->where('id',1)
            ->find();
        $this->assign('info',$info);
        $this->assign('seo_title', $info['seo_title']);
        $this->assign('seo_keywords',$info['seo_keywords']);
        $this->assign('seo_describe',$info['seo_describe']);
        return $this->fetch();
    }

    /**
     * 获取首页轮播图
     * @return mixed
     */
    private function getBanner(){
        $list = db('banner')
            ->where('status',1)
            ->order('sort','asc')
            ->select();
        return $list;
    }

    /**
     * 发送询盘信息
     * @param $data
     * @return \think\response\Json
     */
    public function sendInquiryMessage($data){
        $data['create_time'] = time();
        $data['update_time'] = time();

        //系统设置收到询盘消息是否发送邮件通知
        $is_send_email = get_system_val('inquiry_send_email');

        $result = db('inquiry')
            ->data($data)
            ->insert();
        if ($result){
            if ($is_send_email == 1){
                $this->sendEmail();
            }
            return json(['code' => 'suc', 'msg' => 'Thank you for your feedback, we will contact you as soon as possible!']);
        }else {
            return json(['code' => 'err', 'msg' => 'The system is busy, Please try again.']);
        }
    }

    public function inquirySuccess(){

        return $this->fetch();
    }
}